package com.jetgege.smartbox.Me;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jetgege.smartbox.Base.BaseActivity;
import com.jetgege.smartbox.R;
import com.jetgege.smartbox.Tools.Common;
import com.kaopiz.kprogresshud.KProgressHUD;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.OnClick;

public class NewPWDActivity extends BaseActivity {

    @BindView(R.id.newpwd_toolbar)
    Toolbar newpwdToolbar;
    @BindView(R.id.et_SetNewPWD)
    EditText etSetNewPWD;
    @BindView(R.id.ib_SetNewPWDClear)
    ImageButton ibSetNewPWDClear;
    @BindView(R.id.et_SetNewPWDConfirm)
    EditText etSetNewPWDConfirm;
    @BindView(R.id.ib_SetNewPWDConfirmClear)
    ImageButton ibSetNewPWDConfirmClear;
    @BindView(R.id.ll_SetNewPWDConfirm)
    LinearLayout llSetNewPWDConfirm;
    @BindView(R.id.tv_SetNewPWDTips)
    TextView tvSetNewPWDTips;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_new_pwd;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initValue();
    }

    private void initValue() {

        newpwdToolbar.setTitle("新密码");
        setSupportActionBar(newpwdToolbar);
        newpwdToolbar.setNavigationIcon(R.drawable.bar_back_btn);
        newpwdToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        //调出键盘
        Timer timer = new Timer(true);
        timer.schedule(new TimerTask() {
            public void run() {
                InputMethodManager inputManager = (InputMethodManager) etSetNewPWD.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.showSoftInput(etSetNewPWD, 0);
            }
        }, 400);
        imputView();
    }

    private void imputView() {
        etSetNewPWD.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 0) {
                    ibSetNewPWDClear.setVisibility(View.VISIBLE);
                } else ibSetNewPWDClear.setVisibility(View.GONE);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        etSetNewPWDConfirm.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 0) {
                    ibSetNewPWDConfirmClear.setVisibility(View.VISIBLE);
                } else ibSetNewPWDConfirmClear.setVisibility(View.GONE);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.new_pwd, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        modifyTips();
        return true;
    }

    private void modifyTips() {
        final KProgressHUD newpwdHUD = KProgressHUD.create(this)
                .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                .setLabel("设置成功...")
                .setCancellable(true)
                .show();
        Common.dismissHUD(newpwdHUD);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                finish();
            }
        }, 1200);
    }

    @OnClick({R.id.ib_SetNewPWDClear, R.id.ib_SetNewPWDConfirmClear})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ib_SetNewPWDClear:
                etSetNewPWD.setText("");
                break;
            case R.id.ib_SetNewPWDConfirmClear:
                etSetNewPWDConfirm.setText("");
                break;
        }
    }
}
